package com.guangjian.asc.retail.web;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.roo.addon.web.mvc.controller.RooWebScaffold;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.guangjian.asc.common.web.BaseController;
import com.guangjian.asc.retail.domain.OrderDetail;
import com.guangjian.asc.retail.domain.OrderTemplate;
import com.guangjian.asc.retail.domain.OrderTemplateItem;
import com.guangjian.asc.retail.domain.SalesOrder;

@RooWebScaffold(path = "sales-order", formBackingObject = SalesOrder.class)
@RequestMapping("/sales-order")
@Controller
public class SalesOrderController extends BaseController {

	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public String show(@PathVariable("id") Long id, Model model) {
		SalesOrder salesOrder = SalesOrder.findSalesOrder(id);

		model.addAttribute("salesorder", salesOrder);
		model.addAttribute("itemId", id);

		// ---- 算出单据合计 -----------------------START
		Set<OrderDetail> orderDetails = salesOrder.getOrderDetails();
		BigDecimal total = new BigDecimal(0);
		for (OrderDetail orderDetail : orderDetails) {

			BigDecimal single = orderDetail.getActualPrice();
			if (single == null)
				single = new BigDecimal(0);

			single = single.multiply(new BigDecimal(orderDetail.getAmount()));
			total = total.add(single);
		}
		model.addAttribute("totalRow", total.toString());
		// ---- 算出单据合计 -----------------------END

		model.addAttribute("orderdetails", orderDetails);

		return "sales-order/show";
	}

	@RequestMapping(method = RequestMethod.POST)
	public String create(@Valid SalesOrder salesOrder, BindingResult result, Model model, HttpServletRequest request) {
		if (result.hasErrors()) {
			model.addAttribute("salesOrder", salesOrder);
			return "sales-order/create";
		}
		salesOrder.persist();

		// 按照单据模板生成单据明细
		OrderTemplate orderTemplate = salesOrder.getOrderTemplate();
		Set<OrderTemplateItem> orderTemplateItems = orderTemplate.getOrderTemplateItems();
		for (OrderTemplateItem orderTemplateItem : orderTemplateItems) {
			OrderDetail orderDetail = new OrderDetail();

			orderDetail.setSalesOrder(salesOrder);
			orderDetail.setProduct(orderTemplateItem.getProduct());
			orderDetail.setAmount(1);
			orderDetail.setActualPrice(orderTemplateItem.getSalePrice());
			orderDetail.persist();
		}

		return "redirect:/sales-order/" + encodeUrlSegment(salesOrder.getId().toString(), request);
	}

	@RequestMapping(value = "/{id}", params = "print", method = RequestMethod.GET)
	public String printForm(@PathVariable("id") Long id, Model model) {
		SalesOrder salesOrder = SalesOrder.findSalesOrder(id);

		model.addAttribute("salesorder", salesOrder);
		model.addAttribute("itemId", id);

		// ---- 算出单据合计 -----------------------START
		Set<OrderDetail> orderDetails = salesOrder.getOrderDetails();
		BigDecimal total = new BigDecimal(0);
		for (OrderDetail orderDetail : orderDetails) {

			BigDecimal single = orderDetail.getActualPrice();
			if (single == null)
				single = new BigDecimal(0);

			single = single.multiply(new BigDecimal(orderDetail.getAmount()));
			total = total.add(single);
		}
		model.addAttribute("totalRow", total.toString());
		// ---- 算出单据合计 -----------------------END

		model.addAttribute("orderdetails", orderDetails);

		return "sales-order/print";
	}
}
